package com.itwlj.mapper;

import com.github.pagehelper.Page;
import com.itwlj.annotation.AutoFill;
import com.itwlj.dto.GoodsPageQueryDTO;
import com.itwlj.entity.Comment;
import com.itwlj.entity.Goods;
import com.itwlj.enumeration.OperationType;
import com.itwlj.vo.GoodsVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface GoodsMapper {

    /**
     * 根据子分类id查询对应的商品id
     *
     * @param subCategoryIds
     * @return
     */
    List<Long> getGoodsIdsBySubCategoryIds(List<Long> subCategoryIds);

    /**
     * 新增商品
     *
     * @param goods
     */
    @AutoFill(OperationType.INSERT)
    void insert(Goods goods);

    /**
     * 商品分页查询
     *
     * @param goodsPageQueryDTO
     * @return
     */
    Page<GoodsVO> pageQuery(GoodsPageQueryDTO goodsPageQueryDTO);

    /**
     * 商品批量删除
     *
     * @param ids
     */
    void deleteBatch(List<Long> ids);

    /**
     * 根据id修改商品
     *
     * @param goods
     */
    @AutoFill(OperationType.UPDATE)
    void update(Goods goods);

    /**
     * 根据子分类id查询商品
     *
     * @param subCategoryId
     * @return
     */
    @Select("select id, name, `desc`, price, picture from goods where sub_category_id = #{subCategoryId} and status = 1 order by sales_count")
    List<Goods> getBySubCategoryId(Long subCategoryId);

    /**
     * 根据商品名称模糊查询
     *
     * @param keyword
     * @return
     */
    @Select("select id, name, `desc`, price, picture from goods where name like concat('%',#{keyword},'%')")
    List<Goods> getByNameLike(String keyword);

    /**
     * 根据id查询商品
     *
     * @param id
     * @return
     */
    @Select("select id, name, sub_category_id, spu_code, `desc`, price, old_price, brand, picture, status, sales_count, comment_count, create_time, update_time, create_user, update_user from goods where id = #{id}")
    Goods getById(Long id);

    /**
     * 新增评论
     *
     * @param comment
     */
    @Insert("insert into comment(goods_id, order_id, user_name, content, rating) VALUES (#{goodsId},#{orderId},#{userName},#{content},#{rating})")
    void insertComment(Comment comment);

    /**
     * 根据商品id查询评论
     *
     * @param goodsId
     * @return
     */
    @Select("select * from comment where goods_id = #{goodsId}")
    List<Comment> getCommentByGoodsId(Long goodsId);

    /**
     * 根据订单id查询评论
     *
     * @param orderId
     * @return
     */
    @Select("select * from comment where order_id = #{orderId}")
    List<Comment> getCommentByOrderId(Long orderId);
}
